package pl.edu.amu.wmi.kino.visualjavafx.model.animation.transformations;

import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import pl.edu.amu.wmi.kino.visualjavafx.model.animation.interpolators.Interpolation;
import pl.edu.amu.wmi.kino.visualjavafx.model.objects.VisualObject;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.B6E3E8D1-C37C-1206-EE57-B2682BD11BAB]
// </editor-fold> 
public class ChangeFillColor implements Transformation ,java.io.Serializable{

    // Code block for changeListeners support start
    protected PropertyChangeSupport propertySupport= new PropertyChangeSupport(this);
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
    // Code block for changeListeners support end

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.D282111E-8D8A-184A-D670-F27C88937437]
    // </editor-fold> 
    public ChangeFillColor () {
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.E7BABA0B-C90E-89B6-AADE-E094CCEA85FD]
    // </editor-fold> 
    @Override
    public Interpolation getInterpolation () {
        return this.myInterpolations[0];
    }

    @Override
    public Interpolation[] getInterpolations() {
        return this.myInterpolations;
    }


    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.79D522F1-10AE-862E-0782-8849D2C1A795]
    // </editor-fold> 
    @Override
    public void setInterpolation (Interpolation i) {
        Interpolation oldVal = this.myInterpolations[0];
        this.myInterpolations[0]=i;
        this.myInterpolations[1]=i;
        this.myInterpolations[2]=i;
        propertySupport.firePropertyChange( "Inerpolation" , oldVal, i);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.E594392F-31C0-F159-4F11-193D0FCF135F]
    // </editor-fold> 
    //TODO : Grzegorz : write a class that handles these Color conversions ?
    @Override
    public VisualObject transform (double howMuch, VisualObject transformable,VisualObject orginalTransformedObject) {
        if((transformable instanceof Fillable)==false){
            return transformable;
        }

        Fillable res =(Fillable) transformable;

        double r = res.getFillColor().getRed();
        double g = res.getFillColor().getGreen();
        double b = res.getFillColor().getBlue();

        r=this.myInterpolations[0].getValue(howMuch, r, this.destinedColor.getRed());
        g=this.myInterpolations[1].getValue(howMuch, g, this.destinedColor.getGreen());
        b=this.myInterpolations[2].getValue(howMuch, b, this.destinedColor.getBlue());

        res.setFillColor(new Color((int)r,(int)g,(int)b));

        return (VisualObject)res;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.CE0A356E-2367-5D8A-0D19-4A2C0D89978A]
    // </editor-fold> 
    public Color getDestinedColor () {
        return this.destinedColor;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.1BA88F4E-1ADF-241B-A703-851D1347B3D3]
    // </editor-fold> 
    public void setDestinedColor (Color val) {
        Color oldVal = this.destinedColor;
        this.destinedColor=val;
        propertySupport.firePropertyChange( "destinedColor" , oldVal, val);
    }

    private Interpolation[] myInterpolations=new Interpolation[3];
    private Color destinedColor=Color.WHITE;


}

